#pragma once
#pragma once
#pragma once
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
#include<time.h>
#include "bTree.h"
#include "Record.h"
#pragma once
#pragma warning(disable:4996)
typedef long long ll;
using namespace std;
////////////////////////////////
enum { a, b, c, d, e, f, g };

//typedef struct {
//	int id;//借书的历史编号（比如是第几次借书
//	int ReaderId;
//	int BookId;
//	time_t StartBorrowTime;//开始借书的时间
//	time_t LatestReTime;//最晚的理应归还的时间，超过这个时间就会产生罚款
//}BookRecord;
////////////////////////////////

//对书库的更改
void ModifyBookR(Book b, int n)//n=1表示书存量+1；n=0表示书存量-1
{
	Book bk;
	fstream f;
	f.open(filename, ios::out | ios::binary | ios::in | ios::ate);
	if (!f)
	{
		cout << "打开Book.dat文件错误！" << endl;
	}
	long pos = b.BookID * sizeof(Book);
	//书一定要存在！！
	f.seekp(pos, ios::beg);
	char* temp = new char[sizeof(Book)];
	f.read(temp, sizeof(Book));
	bk = *((Book*)temp);
	if (n == 1)
	{
		bk.SumBook++;
		//cout << endl << bk.SumBook << endl;
	}
	else
	{
		bk.SumBook--;
		//cout << endl << bk.SumBook << endl;
	}
	f.seekp(pos, ios::beg);
	f.write((char*)&bk, sizeof(Book));
	f.close();
}

void inwrong()
{
	/*char s[50];
	gets(s);*/
	//system("cls");//打算清屏的，但是书表不能删
	printf("\t\t\t输入错误！！\n");
	system("PAUSE");
	//system("cls");
}

int Is_bh(int bh, int n)//编号判断
{
	for (int i = 1; i <= n; i++)
	{
		if (bh == i)
			return 1;
	}

	cout << "编号错误！\n";
	return 0;

}



bool borrow_b(vector<Book> B, int readerID)//返回0=返回上一级   readerID=-1表示游客
{
	system("cls");
	int ff;
	int bh;
	int f = 1;
	if (readerID == -1)
	{
		f = 0;
		showBook(B);
		system("PAUSE");
	}

	while (f)
	{
		//有书的话
		cout << "**********************************************************" << endl;
		showBook(B);//展示书籍
		cout << "库存为1的不给借阅！\n";
		cout << "输入0返回上一级！\n";
		cout << "**********************************************************" << endl;
		cout << "输入正确编号即可借阅，请输入：\n";
		cin >> bh;
		while (cin.fail()) {   //或者写成 (0 == y)  也可以不用异常，直接在cin >> y 之后加入cin.fail()判断
			cin.clear();
			cin.ignore();
			cout << "重新输入：";
			cin >> bh;
		}
		//cout << "B.size()" << B.size();
		if (bh == 0)
		{
			f = 0;
			break;
		}
		ff = Is_bh(bh, B.size());//判断编号是否正确
		if (!ff)
		{
			//system("cls");
			continue;
		}
		bh--;
		if (B[bh].SumBook == 1 || B[bh].SumBook < 1)
		{
			cout << "库存不够，借阅失败！\n";
			system("PAUSE");
			system("cls");
			continue;
		}
		break;
	}
	if (f)
	{
		Book NB = B[bh];//找到该书
		ModifyBookR(NB, 0);//书库更改
		int id = NB.BookID;

		BorrowBook bb;
		time_t n = time(NULL);
		if (!SetRecordID(bb, id, n))//书籍的id
		{
			cout << "借书记录生成出错啦！" << endl;
			return 1;
		}

		bb.ReaderId = readerID;
		bb.BookId = id;
		bb.BorrowBookTime = n;
		bb.LatestReTime = n;
		//在这里设置最晚的归还书时间；当前+最大借阅时间
		bb.LatestReTime += 30 * 24 * 3600;
		if (!AddBookRecord(bb))
		{
			cout << "借书记录生成出错啦！" << endl;
			return 1;
		}
		system("cls");
		cout << "借书成功" << endl;
		cout << "借书记录如下所示：" << endl;
		cout << "***************************************************************************" << endl;
		showBorrowRecord(getRecordBybookID(bb.BookId), false);
		cout << "***************************************************************************" << endl;
		cout << "借阅成功！\n" << endl;
		printf("请选择是否继续借书：\n1.返回主页面 \n2.继续借书\n");
		int chooooose;
		printf("请选择:");
		cin >> chooooose;
		if (chooooose == 1)//改了
			return 1;
		else
			return 0;
	}
	return 0;
}


void borrow_name(int rid)
{
	vector<Book> B;
	string value;
	string f = "#";

	cin.ignore();
	cout << "输入书籍名字：\n";
	getline(cin, value);


	B = findBookByValue(value, 1);
	if (B.size() == 0)//没有找到书的话
	{
		cout << "没有找到相关书籍！\n";
		system("PAUSE");
		return;
	}
	int i = borrow_b(B, rid);
}

void borrow_AuthorName(int rid)
{
	vector<Book> B;
	string value;

	cin.ignore();
	cout << "输入作者：\n";
	getline(cin, value);

	B = findBookByValue(value, 2);
	if (B.size() == 0)//没有找到书的话
	{
		cout << "没有找到作者！\n";
		system("PAUSE");
		return;
	}
	int i = borrow_b(B, rid);

}


void borrow_Publisher(int rid)
{
	vector<Book> B;
	string value;

	cin.ignore();
	cout << "输入出版社：\n";
	getline(cin, value);
	B = findBookByValue(value, 3);
	if (B.size() == 0)//没有找到书的话
	{
		cout << "没有找到出版社！\n";
		system("PAUSE");
		return;
	}
	int i = borrow_b(B, rid);

}

void borrow_ISBN(int rid)
{
	vector<Book> B;
	string value;

	cin.ignore();
	cout << "输入ISBN：\n";
	getline(cin, value);

	B = findBookByISBN(value);
	if (B.size() == 0)//没有找到书的话
	{
		cout << "没有找到相关书籍！\n";
		system("PAUSE");
		return;

	}
	int i = borrow_b(B, rid);


}

void borrow_BookID(int rid)
{
	vector<Book> B;
	int value;
	//string f = "#";

	cout << "输入书籍ID：\n";
	cin >> value;
	while (value < 0 || cin.fail()) {
		cin.clear();
		cin.ignore(std::numeric_limits< streamsize >::max(), '\n');
		cout << "*输入错误，请重新输入！" << endl;
		cin >> value;
	}
	cin.ignore();//
	B = FindBookById(value);
	if (B.size() == 0)//没有找到书的话
	{
		cout << "没有找到相关书籍！\n";
		system("PAUSE");
		return;
	}
	int i = borrow_b(B, rid);

}

void borrow_fuzzy(int rid)
{
	vector<Book> B;
	string value;
	cin.ignore();
	cout << "输入关键词：\n";
	getline(cin, value);

	B = fuzzy(value);
	if (B.size() == 0)//没有找到书的话
	{
		cout << "没有找到相关书籍！返回上一级！\n";
		system("PAUSE");
		return;
	}
	int i = borrow_b(B, rid);

}

void borrow_book(int rid)
{

	int i = 1;
	//查书
	while (i)
	{
		system("cls");
		int s = 0;
		cout << "**********************************************************************" << endl;
		cout << "请选择查询方式：①-书籍名字  ②-作者  ③-出版社  ④-ISBN  ⑤-书籍ID  ⑥-模糊搜索  ⑦-退出\n";//6还没有写
		cin >> s;//
		while (s <= 0 || cin.fail()) {
			cin.clear();
			cin.ignore(std::numeric_limits< streamsize >::max(), '\n');
			cout << "*输入错误，请重新输入！" << endl;
			cin >> s;
		}
		//cin.ignore();// .

		system("cls");
		if (s == 1 || s == 2 || s == 3 || s == 4 || s == 5 || s == 6 || s == 7)
		{
			switch (s)
			{
			case 1:
				borrow_name(rid);
				break;
			case 2:
				borrow_AuthorName(rid);
				break;
			case 3:
				borrow_Publisher(rid);
				break;
			case 4:
				borrow_ISBN(rid);
				break;
			case 5:
				borrow_BookID(rid);
				break;
			case 6:
				borrow_fuzzy(rid);
				break;
			case 7:
				i = 0;
				break;
			}
		}
		else
		{
			cout << "输入错误！\n";
			//system("PAUSE");
			cin.ignore();
			continue;
		}
		if (!i)
		{
			break;
		}
	}



}

void t_findbook()
{
	borrow_book(-1);
}